package com.itheima.sh.controller;

import com.itheima.sh.clients.UserFeign;
import com.itheima.sh.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("feign")
public class FeignController {
    @Autowired
    UserFeign userFeign;

    @GetMapping("{id}")
    /*
    * //@HystrixCommand(fallbackMethod = "queryByIdFallBack")
    *   @SpringCloudApplication优先找到这儿的降级方法，不会去找feign整合的降级方法，只能二选一
    *   注销后，展示效果
    * */

    public User queryById(@PathVariable("id") String id){
        //精确控制，访问成功或失败 ，展示熔断效果（默认20次，close状态）
        if(id.equals("itheima")) {
            throw new RuntimeException("用户名不存在");
        }
        User user = userFeign.queryById(id);

        return user;
    }

    /*
    *   降级方法
    * */
//    public User queryByIdFallBack(String id) {
//        User user = new User();
//        user.setName("-1");
//        user.setUsername(id);
//        user.setNickName("网络波动，暂停服务");
//        return user;
//    }

}
